package org.aristotle.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.demo.model.entity.Book;
import org.aristotle.demo.model.entity.Category;
import org.aristotle.demo.mapper.BookMapper;
import org.aristotle.demo.model.entity.Favorite;
import org.aristotle.demo.service.IBookService;
import org.aristotle.demo.service.ICategoryService;
import org.aristotle.demo.model.vo.BookVO;
import org.aristotle.demo.model.vo.CategoryVO;
import org.aristotle.demo.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IFavoriteService favoriteService;


    @Override
    public Page<BookVO> queryPageList(Long id, String title, Long categoryId, Integer current, Integer size) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper();

        if (id != null) {
            queryWrapper.eq("id",id);
        }

        if (!StringUtils.isBlank(title)) {
            queryWrapper.like("title",title);
        }

        if (categoryId != null) {
            queryWrapper.eq("category_id",categoryId);
        }

        Page<Book> page = this.page(new Page<Book>(current,size), queryWrapper);

        // 转为VO
        Page<BookVO> result = AristotleBeanUtils.transitionBean(page, Page<BookVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),BookVO::new));

        result.getRecords().forEach(bookVO -> {
            CategoryVO categoryVO = this.categoryService.getById(bookVO.getCategoryId());
            bookVO.setCategory(categoryVO);
        });
        return result;
    }

    @Override
    public BookVO getById(Long id) {
        Book book = this.baseMapper.selectById(id);

        BookVO bookVO = AristotleBeanUtils.transitionBean(book, BookVO::new);
        CategoryVO categoryVO = categoryService.getById(bookVO.getCategoryId());
        bookVO.setCategory(categoryVO);
        return bookVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        /*删除中间表 Favorite*/
        QueryWrapper<Favorite> queryWrapperFavorite = new QueryWrapper<>();
        queryWrapperFavorite.eq("book_id",id);

        return super.removeById(id) && favoriteService.remove(queryWrapperFavorite);
    }

    @Override
    public Boolean removeCategoryId(Long id) {
        UpdateWrapper<Book> updateWrapper = new UpdateWrapper();
        updateWrapper.set("category_id",null);
        updateWrapper.eq("category_id",id);
        return this.update(updateWrapper);
    }
}
